package user

import (
	"common"
	"common/grpcx"
	"common/json_response"
	"common/zapx"
	"gateway/internal/logic"
	"gateway/internal/pb"
	"gateway/internal/types/v1/user"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

type UserController struct {
	logic *logic.UserLogic
}

// NewUserController 创建用户控制器对象
func NewUserController() *UserController {
	return &UserController{
		logic: logic.NewUserLogic(),
	}
}

// Register 注册用户
func Register(c *gin.Context) {
	var req user.RegisterRequest
	response := json_response.NewJsonHttpResponse(c)
	if err := c.ShouldBindJSON(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	if err := logic.Register(req.Email, req.Code, req.Password); err != nil {
		zapx.Logger.Error("注册失败了", zap.Error(err))
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetMsg("注册成功").Send()
	return
}

// Login 用户登录
func (uc *UserController) Login(c *gin.Context) {
	var req user.LoginRequest
	response := json_response.NewJsonHttpResponse(c)
	if err := c.ShouldBind(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	conn, err := grpcx.NewNoCertConn("user")
	if err != nil {
		zapx.Logger.Error("链接用户服务失败", zap.Error(err))
		response.SetCode(json_response.CODE_ERROR).SetMsg("系统错误[1]").Send()
		return
	}
	defer conn.Close()
	client := pb.NewUserClient(conn)
	ctx, cancel := common.WithTimeoutContext(time.Second * 2)
	defer cancel()
	res, err := client.Login(ctx, &pb.UserLoginRequest{
		Email:    req.Email,
		Password: req.Password,
	})
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(grpcx.GetGrpcErrorMessage(err)).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetMsg("登录成功").SetData(&user.LoginResponse{
		Token:    res.Token,
		NickName: res.User.NickName,
		Avatar:   common.ParseUri(res.User.Avatar),
	}).Send()
	return
}

func GetProfile(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	ctxUserId, ok := ctx.Get("USER_ID")
	if !ok {
		response.SetCode(json_response.CODE_UNAUTHORIZED).SetStatusCode(json_response.CODE_UNAUTHORIZED).SetMsg("未授权").Send()
		return
	}
	u := logic.Profile(ctxUserId.(int32))
	if u == nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg("系统错误").Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(user.ProfileResponse{
		NickName: u.User.NickName,
		Avatar:   common.ParseUri(u.User.Avatar),
	}).Send()
	return
}

func PostProfile(ctx *gin.Context) {
	var req user.EditProfileRequest
	response := json_response.NewJsonHttpResponse(ctx)
	if err := ctx.ShouldBindJSON(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetStatusCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	userId, ok := ctx.Get("USER_ID")
	if !ok {
		response.SetCode(json_response.CODE_UNAUTHORIZED).SetStatusCode(json_response.CODE_UNAUTHORIZED).SetMsg("未授权").Send()
		return
	}
	if err := logic.EditProfile(userId.(int32), &req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetMsg("修改成功").Send()
	return
}
